//
//  ShouHouViewController.m
//  JiuJiuShangCheng
//
//  Created by jiujiukeji on 2018/7/16.
//  Copyright © 2018年 jiujiukeji. All rights reserved.
//

#import "PingJiaDetailViewController.h"
#import "OrderTableViewCell.h"
#import "ShouHouTableViewCell.h"
#import "UITextView+Extension.h"
#import "MyStarView.h"
#import "TZTestCell.h"
#import "TZImagePickerController.h"
#import "DPPhotoGroupViewController.h"
#import "HPPhotoBrowser.h"

@interface PingJiaDetailViewController ()<UITableViewDelegate,UITableViewDataSource,TZImagePickerControllerDelegate,UICollectionViewDelegate, UICollectionViewDataSource,DPPhotoGroupViewControllerDelegate,UITextFieldDelegate,UITextViewDelegate>
{
    NSMutableArray *_selectedAssets;
    BOOL _isSelectOriginalPhoto;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) MyStarView *starsView;
@property (nonatomic, strong) NSMutableArray *selectedPhotos;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSString *star;
@property (nonatomic, strong) NSMutableDictionary *infoDic;
@property (nonatomic, strong) NSMutableArray *filePathArray;

@end

@implementation PingJiaDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = tableViewBgColor;
    [self loadBaseTitleLable:@"商品评价"];
    self.star = @"5";
    self.infoDic = [NSMutableDictionary dictionary];
    self.filePathArray = [NSMutableArray array];
    
    [self.view addSubview:self.tableView];
    [self getPingJiaDetail];
}
#pragma mark - 评价详情
- (void)getPingJiaDetail
{
    if (self.CartId) {
        NSString *code = [NSString stringWithFormat:@"%@%@",safeCode,[GetTimestamp getNowTimeTimestamp]];

        [MBProgressHUD showMessage:nil toView:self.view];
        [TFNetworking getWithURLString:[NSString stringWithFormat:@"%@/api/json/member/order.ashx?action=ordergoodcomment&companyid=%@&code=%@&timestamp=%@&cartid=%@&from=mobile",kServerURL,companyId,[MD5Encrypt  MD5ForLower32Bate:code],[GetTimestamp getNowTimeTimestamp],self.CartId] parameters:nil success:^(id responseObject) {
            [MBProgressHUD hideHUDForView:self.view];
            
            if ([responseObject[@"status"] intValue] == 1) {
                if ([responseObject[@"cid"] intValue] == 0) {
                    // 未提交过评价
                    
                } else if ([responseObject[@"cid"] intValue] > 0) {
                    // 提交过评价
                    self.infoDic = [NSMutableDictionary dictionaryWithDictionary:responseObject];
                    self.selectedPhotos = [NSMutableArray array];
                    NSArray *array = [responseObject[@"commentImages"] componentsSeparatedByString:@"|"];
                    for (NSString *str in array) {
                        [self.selectedPhotos addObject:[NSString stringWithFormat:@"http:%@",str]];
                    }
                    [self.tableView reloadData];
                    [self.collectionView reloadData];
                }
            } else {
                //                [TFHUD addRemindHUD:self.view message:responseObject[@"returnMsg"]];
            }
            
        } failure:^(NSError *error) {
            [TFHUD addRemindHUD:self.view message:@"网络不给力..."];
            
        }];
    }
}
#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        OrderTableViewCell *cell = [[OrderTableViewCell alloc] init];
        cell.contentView.backgroundColor = UIColorWhite;
        [cell.goodsImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http:%@",self.infoDic[@"CartImg"]]]];
        cell.goodsTitleLB.text = self.infoDic[@"CommodityName"];
//        cell.priceLB.text = [NSString stringWithFormat:@"￥%.2f",[self.infoDic[@"CommodityXPrice"] floatValue]];
        cell.describeLB.text = self.infoDic[@"CommodityProperty"];
        cell.numLB.text = [NSString stringWithFormat:@"×%@",self.infoDic[@"CommodityNumber"]];
        cell.selectionStyle = NO;
        return cell;
    } else {
        UITableViewCell *cell = [[UITableViewCell alloc] init];
        if (indexPath.section == 1) {
            // 评分
            [cell.contentView addSubview:self.starsView];
            [self.starsView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(cell.contentView).offset(rateWidth(25));
                make.centerY.equalTo(cell.contentView);
                make.size.mas_offset(CGSizeMake(135, 35));
            }];
            if ([self.infoDic[@"commentLevel"] intValue] == 0) {
                [_starsView.star1 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
                [_starsView.star2 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
                [_starsView.star3 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
                [_starsView.star4 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
                [_starsView.star5 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)]; // wuxingjin
                
            } else if ([self.infoDic[@"commentLevel"] intValue] == 1) {
                [_starsView.star1 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
                [_starsView.star2 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
                [_starsView.star3 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
                [_starsView.star4 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
                [_starsView.star5 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)]; // wuxingjin
                
            } else if ([self.infoDic[@"commentLevel"] intValue] == 2) {
                [_starsView.star1 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
                [_starsView.star2 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
                [_starsView.star3 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
                [_starsView.star4 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
                [_starsView.star5 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)]; // wuxingjin
                
            } else if ([self.infoDic[@"commentLevel"] intValue] == 3) {
                [_starsView.star1 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
                [_starsView.star2 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
                [_starsView.star3 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
                [_starsView.star4 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)];
                [_starsView.star5 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)]; // wuxingjin
                
            } else if ([self.infoDic[@"commentLevel"] intValue] == 4) {
                [_starsView.star1 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
                [_starsView.star2 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
                [_starsView.star3 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
                [_starsView.star4 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
                [_starsView.star5 setImage:UIImageNamed(@"wuxinghui") forState:(UIControlStateNormal)]; // wuxingjin
            } else if ([self.infoDic[@"commentLevel"] intValue] == 5) {
                [_starsView.star1 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
                [_starsView.star2 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
                [_starsView.star3 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
                [_starsView.star4 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)];
                [_starsView.star5 setImage:UIImageNamed(@"wuxingjin") forState:(UIControlStateNormal)]; // wuxingjin
            }
            
        } else if (indexPath.section == 2) {
            // 标签
            UITextField *textField = [[UITextField alloc] init];
            textField.font = [UIFont systemFontOfSize:14];
//            textField.placeholder = @"请输入标签...";
            [cell.contentView addSubview:textField];
            [textField mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(cell.contentView).offset(rateWidth(25));
                make.centerY.equalTo(cell.contentView);
                make.size.mas_offset(CGSizeMake(rateWidth(650), rateHeight(60)));
            }];
            textField.delegate = self;
            self.textField = textField;
            self.textField.enabled = NO;
            self.textField.text = self.infoDic[@"commentPQ"];
            
        } else if (indexPath.section == 3) {
            // 心得
            UITextView *textView = [[UITextView alloc] init];
            [textView setBackgroundColor:UIColorWhite];
            [cell.contentView addSubview:textView];
            //模仿cell复用
            [textView setPlaceholderWithText:@"" Color:[UIColor lightGrayColor]];
            textView.font = [UIFont systemFontOfSize:14];
            textView.delegate = self;
            self.textView = textView;
            self.textView.editable = NO;
            [textView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(cell.contentView).offset(rateWidth(25));
                make.right.equalTo(cell.contentView).offset(-rateWidth(25));
                make.top.equalTo(cell.contentView).offset(rateHeight(25));
                make.bottom.equalTo(cell.contentView).offset(-rateHeight(25));
            }];
            self.textView.text = self.infoDic[@"commentContent"];
            
        } else {
            // 晒一晒
            [cell addSubview:self.collectionView];
            [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(cell).offset(rateWidth(54));
                make.top.equalTo(cell).offset(rateHeight(37));
                make.right.equalTo(cell).offset(-rateWidth(50));
                make.height.equalTo(@(rateHeight(500)));
            }];
        }
        cell.selectionStyle = NO;
        return cell;
    }
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self.infoDic setObject:_textField.text forKey:@"biaoQian"];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    [self.infoDic setObject:_textView.text forKey:@"xinDe"];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return rateHeight(210);
    } else if (indexPath.section == 1 || indexPath.section == 2) {
        return rateHeight(110);
    } else if (indexPath.section == 3) {
        return rateHeight(240);
    } else {
        return rateHeight(500);
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.01;
    } else {
        return rateHeight(70);
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return nil;
    } else {
        UIView *bgV = [[UIView alloc] init];
        bgV.backgroundColor = tableViewBgColor;
        NSArray *array = @[@"评分",@"标签",@"心得",@"晒一晒"];
        UILabel *label = [UILabel labelWithText:array[section-1] textColor:UIColorFromRGB(0x323232) fontSize:15];
        [label sizeToFit];
        [bgV addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(rateWidth(25)));
            make.centerY.equalTo(bgV);
        }];
        if (section == 1) {
            UILabel *rightLB = [UILabel labelWithText:@"1-2星差评；3星中评；4-5星好评" textColor:UIColorFromRGB(0x909090) fontSize:13];
            [rightLB sizeToFit];
            [bgV addSubview:rightLB];
            [rightLB mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(bgV).offset(-rateWidth(25));
                make.centerY.equalTo(bgV);
            }];
            
        }
        return bgV;
    }
    
}
#pragma mark - collectionView dataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.selectedPhotos.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TZTestCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TZTestCell" forIndexPath:indexPath];
    cell.videoImageView.hidden = YES;
    
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:self.selectedPhotos[indexPath.row]]];
    cell.asset = _selectedAssets[indexPath.row];
    cell.deleteBtn.hidden = YES;
    cell.contentView.backgroundColor = kClearColor;
    cell.gifLable.hidden = YES;
    
//    cell.deleteBtn.tag = indexPath.row;
//    [cell.deleteBtn addTarget:self action:@selector(deleteBtnClik:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
#pragma mark - collectionView delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 浏览图片
    if (self.selectedPhotos.count != 0) {
        TZTestCell *cell = (TZTestCell *)[collectionView cellForItemAtIndexPath:indexPath];
        // 浏览图片
        [HPPhotoBrowser showFromImageView:cell.imageView inView:[UIApplication sharedApplication].keyWindow withURLStrings:self.selectedPhotos   atIndex:indexPath.row];
    }
}

- (MyStarView *)starsView
{
    if (!_starsView) {
        _starsView = [[MyStarView alloc] initWithFrame:CGRectMake(0, 0, 135, 35)];
    }
    return _starsView;
}
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(rateWidth(120), rateWidth(120));
        flowLayout.minimumInteritemSpacing = 15;
        flowLayout.minimumLineSpacing = 15;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, rateWidth(40), rateHeight(200)) collectionViewLayout:flowLayout];
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.backgroundColor = kClearColor;
        _collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        [_collectionView registerClass:[TZTestCell class] forCellWithReuseIdentifier:@"TZTestCell"];
    }
    return _collectionView;
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, kScreenWidth, kScreenHeight-SafeAreaTopHeight-SafeAreaBottomHeight) style:(UITableViewStyleGrouped)];
        _tableView.backgroundColor = UIColorFromRGB(0xf5f5f5);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = NO;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
    }
    return _tableView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
